﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IISLogAnalyzer
{
    public partial class FormProgress : Form
    {
        FormIISLogAnalyzer mainForm;
        public FormProgress(FormIISLogAnalyzer formIISLogAnalyzer)
        {
            InitializeComponent();
            mainForm = formIISLogAnalyzer;
            IniProgress();
            // 开始异步操作  
            if (!backgroundWorker.IsBusy)
            {
                backgroundWorker.RunWorkerAsync(); // 传递要遍历的起始目录  
            }
        }

        private void IniProgress()
        {
            // 初始化BackgroundWorker  
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.WorkerReportsProgress = true; // 允许报告进度  
            backgroundWorker.WorkerSupportsCancellation = true; // 允许取消操作  

            // 关联事件处理程序  
            backgroundWorker.DoWork += BackgroundWorker_DoWork;
            backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
            backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;

            // 初始化进度条和标签  
            progressBar.Minimum = 0;
            progressBar.Maximum = 100;
            progressLabel.Text = "0% - 0/0";
        }


        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (TraverseDirectoryFiles.filePaths.Count > 0)
            {
                try
                {
                    int totalFiles = TraverseDirectoryFiles.filePaths.Count;
                    for (int i = 0; i < totalFiles; i++)
                    {
                        if (backgroundWorker.CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }

                        // 更新进度（进度条的范围是0-100）  
                        int progressPercentage = (int)((double)i / totalFiles * 100);
                        backgroundWorker.ReportProgress(progressPercentage, i.ToString() + "/" + totalFiles.ToString());

                        mainForm.AnalyzeLog(TraverseDirectoryFiles.filePaths[i], pbFile, lbFile);
                    }
                }
                catch (Exception ex)
                {
                    // 处理异常  
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // 更新UI以反映进度  
            progressBar.Value = e.ProgressPercentage;
            progressLabel.Text = e.ProgressPercentage.ToString() + "% - " + e.UserState.ToString();
        }


        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // 操作完成后的处理  
            if (e.Error != null)
            {
                // 处理错误  
                Logger.WriteLog(e.Error.Message);
            }
            else
            {
                mainForm.BindDate();
                mainForm.BindWebSites();
                this.Close();
            }
        }

    }
}
